/*#########################################################################
# Amparok Server by Saggi Mizrahi.                                        #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
#   This program is distributed in the hope that it will be useful,       #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#   GNU General Public License for more details.                          #
#                                                                         #
#   You should have received a copy of the GNU General Public License     #
#   along with this program; if not, write to the                         #
#   Free Software Foundation, Inc.,                                       #
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         #
##########################################################################*/

Importer.loadQtBinding("qt.core");
Importer.loadQtBinding("qt.xml");
Importer.loadQtBinding("qt.gui");
Importer.loadQtBinding("qt.network");
Importer.loadQtBinding("qt.uitools");


KB = Math.pow(2, 10);
MB = Math.pow(2, 20);

alert = Amarok.alert;
function defaultArg(arg, value)
{
  if ((arg === null) || (arg === undefined))
  {
    return value;
  }
  return arg;
}
function createWindow(uifile)
{
  var UIloader = new QUiLoader( this );
  var uifile = new QFile ( Amarok.Info.scriptPath() + "/" + uifile );
  uifile.open( QIODevice.ReadOnly );
  wnd = UIloader.load( uifile, this);
  uifile.close();
  children = wnd.children();
  for (child in children)
  { 
    c = children[child];
    name = c.objectName;
    if (name)
    {
      wnd[name] = c;
    }
  }
  return wnd;
}

function notify(title, content, timeout)
{
  if (arguments.length < 2)
  {
    content = title;
  }
  if (arguments.length < 3)
  {
    timeout = "";
  }
  p = new QProcess()
  p.start('kdialog', ['--title', title, '--passivepopup', content, timeout]);
}

function listAttrs(obj)
{
  res = "";
  for (attr in obj)
  {
    res += attr + ":" + obj[attr] +"\n";
  }
  alert(res);
}

/* stolen from Ryan Stout http://devers.blogspot.com/2007/09/worlds-smallest-tojson-function.html */
function toJson(obj) {
 switch (typeof obj) {
  case 'object':
   if (obj) {
    var list = [];
    if (obj instanceof Array) {
     for (var i=0;i < obj.length;i++) {
      list.push(toJson(obj[i]));
     }
     return '[' + list.join(',') + ']';
    } else {
     for (var prop in obj) {
      list.push('"' + prop + '":' + toJson(obj[prop]));
     }
     return '{' + list.join(',') + '}';
    }
   } else {
    return 'null';
   }
  case 'string':
   return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
  case 'number':
  case 'boolean':
   return new String(obj);
 }
}

function debug(text)
{
  //notify("Debug", text);
  //alert(text);
}

QByteArray.prototype.toString = function() 
{
    ts = new QTextStream( this, QIODevice.ReadOnly );
    return ts.readAll();
};

function md5(src)
{
  hasher = new QCryptographicHash(QCryptographicHash.Md5);
  hasher.addData(new QByteArray(src));
  return hasher.result();
}

function query(sql)
{
  return Amarok.Collection.query(sql);
}